<?php


$db = sqlite_open("config.db");
$table_general = sqlite_query($db, "SELECT name FROM sqlite_master WHERE name='general' AND type='table'");
if (sqlite_num_rows($table_general) < 1) {
  $table_general = sqlite_query($db, "CREATE TABLE general ('id' INTEGER PRIMARY KEY NOT NULL, 'path' VARCHAR(255), 'host' VARCHAR(255), 'port' VARCHAR(5), 'pass' VARCHAR(100), 'streamport' VARCHAR(5), 'bgcolor' VARCHAR(12), 'fncolor' VARCHAR(12))");
}

$table_users = sqlite_query($db, "SELECT name FROM sqlite_master WHERE name='users' AND type='table'");
if (sqlite_num_rows($table_users) < 1) {
  sqlite_query($db, "CREATE TABLE users ('id' INTEGER PRIMARY KEY NOT NULL, 'name' VARCHAR(12), 'password' VARCHAR(32), 'admin' BOOL)");
}

$table_applications = sqlite_query($db, "SELECT name FROM sqlite_master WHERE name='applications' AND type='table'");
if (sqlite_num_rows($table_applications) < 1) {
  sqlite_query($db, "CREATE TABLE applications ('id' INTEGER PRIMARY KEY NOT NULL, 'name' VARCHAR(12), 'path' VARCHAR(255), 'exe' VARCHAR(32), 'startstring' VARCHAR(255))");
}

$table_library = sqlite_query($db, "SELECT name FROM sqlite_master WHERE name='library' AND type='table'");
if (sqlite_num_rows($table_library) < 1) {
  sqlite_query($db, "CREATE TABLE library ('id' INTEGER PRIMARY KEY NOT NULL, 'name' VARCHAR(12), 'file' VARCHAR(32), 'type' VARCHAR(12), 'pagelimit' INT, 'thumb_width' INT, 'thumb_height' INT, 'streaming' BOOL, 'path' VARCHAR(255), 'umedia_folder' VARCHAR(32))");
}

$table_live = sqlite_query($db, "SELECT name FROM sqlite_master WHERE name='live' AND type='table'");
if (sqlite_num_rows($table_live) < 1) {
  sqlite_query($db, "CREATE TABLE live ('id' INTEGER PRIMARY KEY NOT NULL, 'name' VARCHAR(32), 'alias' VARCHAR(32), 'admin' BOOL)");
}


$page = $_GET['page'];
echo '<center><b style="font-size: 20;">WeebUI Setup</b></center><br>';
echo "<div align=\"center\" style=\"border: 1px solid black; padding: 20px; margin: 20px;\">";
echo "<b>-<a href=\"index.php?section=setup&page=general\">General</a></b><br>";
echo "<b>-<a href=\"index.php?section=setup&page=users\">Users</a></b><br>";
echo "<b>-<a href=\"index.php?section=setup&page=library\">Libraries</a></b><br>";
echo "<b>-<a href=\"index.php?section=setup&page=live\">Live Library</a></b><br>";
echo "<b>-<a href=\"index.php?section=setup&page=applications\">Applications</a></b><br>";
echo "</div>";

$sql_users = sqlite_query($db, "SELECT * FROM users WHERE admin='1'");
if ((sqlite_num_rows($sql_users) > 0) && (!session_is_registered('admin'))) exit();

if ($_GET['update'] == "general") {
  $path = str_replace("\\\\", "\\", $_POST['path']);
  $host = $_POST['host'];
  $port = $_POST['port'];
  $pass = $_POST['pass'];
  $streamport = $_POST['streamport'];
  $bgcolor = $_POST['bgcolor'];
  $fncolor = $_POST['fncolor'];
  sqlite_query($db, "DELETE FROM general");
  sqlite_query($db, "INSERT INTO general (path,host,port,pass,streamport,bgcolor,fncolor) VALUES('$path','$host','$port','$pass','$streamport','$bgcolor','$fncolor')");
}

if ($_GET['add'] == "user") {
  $name = $_POST['name'];
  $pass = md5($_POST['password']);
  $admin = $_POST['admin'];
  if ((!$name) || (!$_POST['password'])) $usr_error = "You must fill in both username and password!";
  else {
    if (sqlite_num_rows(sqlite_query($db, "SELECT name FROM users WHERE name='$name'")) > 0) $usr_error = "User already exists!";
    else {
      sqlite_query($db, "INSERT INTO users (name,password,admin) VALUES('$name','$pass','$admin')");
      if ((sqlite_num_rows(sqlite_query($db, "SELECT * FROM users WHERE admin='1'")) > 0) && (!session_is_registered('admin'))) {
        echo "<center>\n";
        echo "<form method=\"post\" action=\"login.php\">\n";
        echo "<b>Username:</b><br>\n";
        echo "<input name=\"user\" type=\"text\"><br>\n";
        echo "<b>Password:</b><br>\n";
        echo "<input name=\"pass\" type=\"password\"><br>\n";
        echo "<input type=\"submit\" value=\"Login\">\n";
        echo "</form>\n";
        echo "</center>\n";
        exit();
      }
    }
  }
}

if ($_GET['delete'] == "user") {
  $id = $_GET['id'];
  if (!$id) exit();
  sqlite_query($db, "DELETE FROM users WHERE id='$id'");
}


if ($_GET['add'] == "library") {
  $name = $_POST['name'];
  $file = $_POST['file'];
  $type = $_POST['type'];
  $pagelimit = $_POST['pagelimit'];
  $thumb_width = $_POST['thumb_width'];
  $thumb_height = $_POST['thumb_height'];
  $streaming = $_POST['streaming'];
  $path = str_replace("\\\\", "\\", $_POST['path']);
  $umedia = $_POST['umedia'];
  if (($name) && ($file) && ($pagelimit) && ($thumb_width) && ($thumb_height)) {
    sqlite_query($db, "INSERT INTO library (name,file,type,pagelimit,thumb_height,thumb_width,streaming,path,umedia_folder)
                       VALUES('$name','$file','$type','$pagelimit','$thumb_height','$thumb_width','$streaming','$path','$umedia')");
  }
}
if ($_GET['update'] == "library") {
  $id = $_GET['id'];
  if ($_POST['delete'] == "Delete") {
    if ($id) sqlite_query($db, "DELETE FROM library WHERE id='$id'");
  }
  else {
    $name = $_POST['name'];
    $file = $_POST['file'];
    $type = $_POST['type'];
    $pagelimit = $_POST['pagelimit'];
    $thumb_width = $_POST['thumb_width'];
    $thumb_height = $_POST['thumb_height'];
    $streaming = $_POST['streaming'];
    $path = str_replace("\\\\", "\\", $_POST['path']);
    $umedia = $_POST['umedia'];
    if (($id) && ($name) && ($file) && ($pagelimit) && ($thumb_width) && ($thumb_height)) {
      sqlite_query($db, "UPDATE library SET name='$name',file='$file',type='$type',pagelimit='$pagelimit',thumb_height='$thumb_height',
                                            thumb_width='$thumb_width',streaming='$streaming',path='$path',umedia_folder='$umedia'
                                            WHERE id='$id'");
    }
  }
}
if ($_GET['add'] == "live") {
  $name = $_POST['name'];
  $alias = $_POST['alias'];
  $a = $_POST['admin'];
  if (($name) && ($alias)) {
    sqlite_query($db, "INSERT INTO live (name,alias,admin) VALUES('$name','$alias','$a')");
  }
  else echo "<b style=\"text-color: red;\">Error: Please fill out all fields for LiveStream!</b><br><br>";
}

if ($_GET['update'] == "live") {
  $id = $_GET['id'];
  if ($_POST['delete'] == "Delete") {
    if ($id) sqlite_query($db, "DELETE FROM live WHERE id='$id'");
  }
  else {
    $name = $_POST['name'];
    $alias = $_POST['alias'];
    $a = $_POST['admin'];
    if (($id) && ($name) && ($alias) && ($a)) {
      sqlite_query($db, "UPDATE live SET name='$name',alias='$alias',admin='$a' WHERE id='$id'");
    }
  }
}

if ($_GET['add'] == "application") {
  $name = $_POST['name'];
  $path = str_replace("\\\\", "\\", $_POST['path']);
  $exe = $_POST['exe'];
  $start = $_POST['startstring'];
  if (($name) && ($exe) && ($path) && ($start)) {
    sqlite_query($db, "INSERT INTO applications (name,path,exe,startstring) VALUES('$name','$path','$exe','$start')");
  }
  else echo "<b style=\"text-color: red;\">Error: Please fill out all fields for application!</b><br><br>";
}

if ($_GET['update'] == "application") {
  $id = $_GET['id'];
  if ($_POST['delete'] == "Delete") {
    if ($id) sqlite_query($db, "DELETE FROM applications WHERE id='$id'");
  }
  else {
    $name = $_POST['name'];
    $path = str_replace("\\\\", "\\", $_POST['path']);
    $exe = $_POST['exe'];
    $start = $_POST['startstring'];
    if (($id) && ($name) && ($exe) && ($path) && ($start)) {
      sqlite_query($db, "UPDATE applications SET name='$name',path='$path',exe='$exe',startstring='$start' WHERE id='$id'");
    }
  }
}


if ($page == "general") {

  $sql_general = sqlite_query($db, "SELECT * FROM general");
  $general = sqlite_fetch_array($sql_general);
  echo '<div width="100%" style="border-bottom: 1px solid black; border-top: 1px solid black; margin-bottom: 20px; padding-top: 15px;">';
  echo '<form method="post" action="index.php?section=setup&update=general&page=general" style="margin: 0px;">';
  echo '<b style="font-size: 18;">General:</b><br><br>';
  echo '<b>Meedio Folder:</b><br>';
  echo '<input type="text" size="40" name="path" value="'.$general['path'].'"><br>';
  echo '<b>Meedio Host: <br>(Host of the machine running meedio, like 192.168.0.4 or yourhost.com)</b><br>';
  echo '<input type="text" name="host" value="'.$general['host'].'"><br>';
  echo '<b>Meedio Port: <br>(Port for Network Client, default: 7500)</b><br>';
  echo '<input type="text" name="port" value="'.$general['port'].'"><br>';
  echo '<b>Meedio Pass: <br>(Network client password, from meedio configuration -&gt; general))</b><br>';
  echo '<input type="text" name="pass" value="'.$general['pass'].'"><br>';
  echo '<b>StreamPort: (Unreal Media Server Port, default: 5119)</b><br>';
  echo '<input type="text" name="streamport" value="'.$general['streamport'].'"><br>';
  echo '<b>Background color: (color code or name, default: white)</b><br>';
  if (!$general['bgcolor']) $general['bgcolor'] = "white";
  echo '<input type="text" name="bgcolor" value="'.$general['bgcolor'].'"><br>';
  echo '<b>Font color: (color code or name, default: black)</b><br>';
  if (!$general['fncolor']) $general['fncolor'] = "black";
  echo '<input type="text" name="fncolor" value="'.$general['fncolor'].'"><br>';
  echo '<br>';
  echo '<input type="submit" value="Update">';
  echo '</form>';
  echo '</div>';
}

if ($page == "users") {
  echo '<table width=100% border>';
  echo '<tr>';
  echo '<td colspan="4"><b style="font-size: 18;">Users:</b><br></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td><b>ID</b></td>';
  echo '<td><b>Username</b></td>';
  echo '<td><b>User/Admin</b></td>';
  echo '<td><b>Action</b></td>';
  echo '</tr>';
  $sql_users = sqlite_query($db, "SELECT * FROM users ORDER BY id ASC");
  if (sqlite_num_rows($sql_users) < 1) echo '<tr><td colspan="4" align="center"><i>no users found</i></td></tr>';
  while ($users = sqlite_fetch_array($sql_users)) {
    echo '<tr>';
    echo '<td align="right">'.$users['id'].'</td>';
    echo '<td>'.$users['name'].'</td>';
    if ($users['admin'] == "1") $state = "admin";
    else $state = "user";
    echo '<td>'.$state.'</td>';
    echo '<td><a href="index.php?section=setup&delete=user&page=users&id='.$users['id'].'">delete</a></td>';
    echo '</tr>';
  }
  echo '<form method="post" action="index.php?section=setup&add=user&page=users">';
  echo '<tr><td colspan="4" align="center" style="padding-top: 20px;"><b style="font-size: 16;">New User:</b><br>';
  echo '<b style="color: red;">'.$usr_error.'</b><br>';
  echo '<b>Username:<b><input type="text" name="name">&nbsp;&nbsp;&nbsp;';
  echo '<b>Password:</b><input type="password" name="password">';
  echo '<select name="admin">';
  echo '<option value="1">Admin</option>';
  echo '<option value="0">User</option>';
  echo '</select><br>';
  echo '<input type="submit" value="Add new user">';
  echo '</td></tr>';
  echo '</form>';
  echo '</table>';
}


if ($page == "library") {
  echo '<div width="100%" style="border-bottom: 1px solid black; border-top: 1px solid black; margin-bottom: 20px; margin-top: 20px; padding-top: 15px;">';
  echo '<b style="font-size: 18;">Libraries:</b><br><br>';

  $sql_libraries = sqlite_query($db, "SELECT * FROM library ORDER BY id ASC");
  if (sqlite_num_rows($sql_libraries) < 1) echo "<i>no libraries found!</i><br><br>";
  while ($l = sqlite_fetch_array($sql_libraries)) {
    $id = $l['id'];
    echo '<form method="post" action="index.php?section=setup&update=library&page=library&id='.$id.'" style="margin: 0px;">';
    echo '<b style="font-size: 16;">'.$l['name'].'</b><br><br>';
    echo '<b>Name:</b><br>';
    echo '<input type="text" name="name" value="'.$l['name'].'"><br>';
    echo '<b>File:</b><br>';
    echo '<input type="text" name="file" value="'.$l['file'].'"><br>';
    echo '<b>Type:</b><br>';
    echo '<select name="type">';
    if ($l['type'] == "music") $s_music = "Selected";
    if ($l['type'] == "movies") $s_movies = "Selected";
    if ($l['type'] == "tv") $s_tv = "Selected";
    echo '<option value="music" '.$s_music.'>Music</option>';
    echo '<option value="movies" '.$s_movies.'>Movie</option>';
    echo '<option value="tv" '.$s_tv.'>TV</option>';
    echo '</select><br>';
    echo '<b>Page limit: (how many items to display pr page!)</b><br>';
    echo '<input type="text" name="pagelimit" value="'.$l['pagelimit'].'"><br>';
    echo '<b>Height of small images (default: movies: 180, music: 150, tv: 110)</b><br>';
    echo '<input type="text" name="thumb_height" value="'.$l['thumb_height'].'"><br>';
    echo '<b>Width of small images (defaul: movies: 130, music: 150, tv: 590)</b><br>';
    echo '<input type="text" name="thumb_width" value="'.$l['thumb_width'].'"><br>';
    echo '<b>Streaming of media:</b><br>';
    echo '<select name="streaming">';
    if ($l['streaming'] == 1) $s_on = "Selected";
    if ($l['streaming'] == 0) $s_off = "Selected";
    echo '<option value="1" '.$s_on.'>Enabled</option>';
    echo '<option value="0" '.$s_off.'>Disabled</option>';
    echo '</select><br>';
    echo '<b>Root of media files, needed for streaming (ex: d:\\movies\\)</b><br>';
    echo '<input type="path" name="path" value="'.$l['path'].'"><br>';
    echo '<b>UnrealMediaServer Virtual Folder (ex: Movies)</b><br>';
    echo '<input type="text" name="umedia" value="'.$l['umedia_folder'].'"><br>';
    echo '<input type="submit" value="Update Library">';
    echo '<input type="submit" name="delete" value="Delete">';
    echo '</form>';
    echo '<br><br>';
    unset($s_tv,$s_movies,$s_music,$s_on,$s_off);
  }
  echo '<br><br>';
  echo '<b style="font-size: 16">Add New Library</b>';
  echo '<form method="post" action="index.php?section=setup&add=library&page=library" style="margin: 0px;">';
  echo '<b>Name:</b><br>';
  echo '<input type="text" name="name"><br>';
  echo '<b>File:</b><br>';
  echo '<input type="text" name="file"><br>';
  echo '<b>Type:</b><br>';
  echo '<select name="type">';
  echo '<option value="music">Music</option>';
  echo '<option value="movies">Movie</option>';
  echo '<option value="tv">TV</option>';
  echo '</select><br>';
  echo '<b>Page limit: (how many items to display pr page!)</b><br>';
  echo '<input type="text" name="pagelimit"><br>';
  echo '<b>Height of small images (default: movies: 180, music: 150, tv: 110)</b><br>';
  echo '<input type="text" name="thumb_height"><br>';
  echo '<b>Width of small images (defaul: movies: 130, music: 150, tv: 590)</b><br>';
  echo '<input type="text" name="thumb_width"><br>';
  echo '<b>Streaming of media:</b><br>';
  echo '<select name="streaming">';
  echo '<option value="1">Enabled</option>';
  echo '<option value="0">Disabled</option>';
  echo '</select><br>';
  echo '<b>Root of media files, needed for streaming (ex: d:\\movies\\)</b><br>';
  echo '<input type="path" name="path"><br>';
  echo '<b>UnrealMediaServer Virtual Folder (ex: Movies)</b><br>';
  echo '<input type="text" name="umedia"><br>';
  echo '<input type="submit" value="Add Library">';
  echo '</form>';
  echo '</div>';
}


if ($page == "live") {
  echo '<div width="100%" style="border-bottom: 1px solid black; border-top: 1px solid black; margin-bottom: 20px; margin-top: 20px; padding-top: 15px;">';
  echo '<b style="font-size: 18;">Live Library:</b><br><br>';
  $sql_live = sqlite_query($db, "SELECT * FROM live ORDER BY id ASC");
  if (sqlite_num_rows($sql_live) < 1) echo "<i>no live-streams found!</i><br><br>";
  while ($live = sqlite_fetch_array($sql_live)) {
    $id = $live['id'];
    echo '<form method="post" action="index.php?section=setup&update=live&page=live&id='.$id.'" style="margin: 0px;">';
    echo '<b style="font-size: 16;">'.$live['name'].'</b><br><br>';
    echo '<b>Name:</b><br>';
    echo '<input type="text" name="name" value="'.$live['name'].'"><br>';
    echo '<b>Alias:</b><br>';
    echo '<input type="text" name="alias" value="'.$live['alias'].'"><br>';
    echo '<b>Only for administrators:</b><br>';
    echo '<select name="admin" type="radio">';
    if ($live['admin'] == 1) $sy = "SELECTED";
    else $sn = "SELECTED";
    echo '<option value="1" '.$sy.'>Yes</option>';
    echo '<option value="0" '.$sn.'>No</option>';
    echo '</select><br>';
    echo '<input type="submit" value="Update LiveStream">';
    echo '<input type="submit" name="delete" value="Delete">';
    echo '</form>';
    echo '<br><br>';
    unset($sn,$sy);
  }
  echo '<b style="font-size: 16">Add New LiveStream</b>';
  echo '<form method="post" action="index.php?section=setup&add=live&page=live" style="margin: 0px;">';
  echo '<b>Name: (ex: Screen Capture)</b><br>';
  echo '<input type="text" name="name"><br>';
  echo '<b>Alias: (ex: Screen)</b><br>';
  echo '<input type="text" name="alias"><br>';
  echo '<b>Only for administrators:</b><br>';
  echo '<select name="admin" type="radio">';
  echo '<option value="1">Yes</option>';
  echo '<option value="0">No</option>';
  echo '</select><br>';
  echo '<input type="submit" value="Add LiveStream">';
  echo '</form>';
  echo "</div>";
}


if ($page == "applications") {
  echo '<div width="100%" style="border-bottom: 1px solid black; border-top: 1px solid black; margin-bottom: 20px; margin-top: 20px; padding-top: 15px;">';
  echo '<b style="font-size: 18;">Applications:</b><br><br>';
  $sql_applications = sqlite_query($db, "SELECT * FROM applications ORDER BY id ASC");
  if (sqlite_num_rows($sql_applications) < 1) echo "<i>no applications found!</i><br><br>";
  while ($app = sqlite_fetch_array($sql_applications)) {
    $id = $app['id'];
    echo '<form method="post" action="index.php?section=setup&update=application&page=applications&id='.$id.'" style="margin: 0px;">';
    echo '<b style="font-size: 16;">'.$app['name'].'</b><br><br>';
    echo '<b>Name: (ex: Meedio)</b><br>';
    echo '<input type="text" name="name" value="'.$app['name'].'"><br>';
    echo '<b>Path: (ex: c:\program files\meedio\meedio essentials)</b><br>';
    echo '<input type="text" name="path" value="'.$app['path'].'"><br>';
    echo '<b>EXE File: (ex: Meedio.exe)</b><br>';
    echo '<input type="text" name="exe" value="'.$app['exe'].'"><br>';
    echo '<b>Start String: (ex: Meedio.exe /parameter1 /parameter2)</b><br>';
    echo '<input type="text" name="startstring" value="'.$app['startstring'].'"><br>';
    echo '<input type="submit" value="Update Application">';
    echo '<input type="submit" name="delete" value="Delete">';
    echo '<input type="submit" value="Add LiveStream">';
    echo '</form>';
    echo '<br><br>';
  }

  echo '<br><br>';
  echo '<b style="font-size: 16">Add New Application</b>';
  echo '<form method="post" action="index.php?section=setup&add=application&page=applications" style="margin: 0px;">';
  echo '<b>Name: (ex: Meedio)</b><br>';
  echo '<input type="text" name="name"><br>';
  echo '<b>Path: (ex: c:\program files\meedio\meedio essentials)</b><br>';
  echo '<input type="text" name="path"><br>';
  echo '<b>EXE File: (ex: Meedio.exe)</b><br>';
  echo '<input type="text" name="exe"><br>';
  echo '<b>Start String: (ex: Meedio.exe /parameter1 /parameter2)</b><br>';
  echo '<input type="text" name="startstring"><br>';
  echo '<input type="submit" value="Add Application">';
  echo '</form>';
  echo '</div>';
}
?>
